﻿function User()
{   
    this.util = new Utility();
    
    this.showLogin = function(event)
    {
        $("#divDimBackground").show();
        $("#divSignup").hide();
        $("#divLogin").show();
        $("#divMembershipBox").show();
        $("#divKickWarning").hide();
        
        
        this.util.moveCenter($("#divMembershipBox"), event.clientY);               
        this.util.maxmize($("#divDimBackground"));
    }
    
    this.showSignUp = function(event)
    {
        $("#divDimBackground").show();
        $("#divSignup").show();
        $("#divLogin").hide();
        $("#divMembershipBox").show();
        $("#divKickWarning").hide();
        
        this.util.moveCenter($("#divMembershipBox"), event.clientY);               
        this.util.maxmize($("#divDimBackground"));       
    }
    
    this.showSubmit = function()
    {
        $("#divDimBackground").show();
        $("#divSignup").hide();
        $("#divLogin").hide();
        $("#divMembershipBox").hide();
        $("#divSubmitStoryBox").show();
        
        this.util.moveCenter($("#divSubmitStoryBox"));               
        this.util.maxmize($("#divDimBackground"));       
    }
    
    this.validateSignUp = function(name, password, repassword, email)
    {
        var signupMessage = $("#signupMessage");
        signupMessage.show();
        if( name == null || name == "" )
        {
            signupMessage.attr("innerText", "Please input your name");
            return false;    
        }
        else if( password == null || password == "" )
        {
            signupMessage.attr("innerText", "Please input the password");
            return false;
        }
        else if( repassword == null || repassword == "" )
        {
            signupMessage.attr("innerText", "Please retype the password");
            return false;
        }
        else if( password != repassword )
        {
            signupMessage.attr("innerText", "Passwords must be the same");
            return false;
        }
        else if( email == null || email == "")
        {
            signupMessage.attr("innerText", "Please input your email");
            return false;
        }
        else{
            return true;
        }
    }
    
    this.hideAllForms = function()
    {
        $("#divDimBackground").hide();
        $("#divSignup").hide();
        $("#divLogin").hide();
        $("#divMembershipBox").hide();
        $("#divSubmitStoryBox").hide();      
    }
    
    this.signUp = function()
    {
        var userName = $("#txtSignupUserName").val();
        var password = $("#txtSignupPassword").val();
        var repassword = $("#txtSignupConfirm").val();
        var email = $("#txtSignupEmail").val();
        
        if( this.validateSignUp(userName, password, repassword, email) == true )
        {
            $.ajax({
                type:"POST",
                url:"/User/SignUp",
                data:"userName="+userName+"&password="+password+"&email="+email,
                success:function(msg){
                    msg = msg.parseJSON();
                    if(msg["isSuccessful"] == false )
                    { 
                        $("#signupMessage").attr("innerText",msg.errorMessage);
                    }
                    else{
                        $("#signupMessage").attr("innerText", "Signup successfully!");
                        window.setTimeout("", 2000); 
                        window.location.reload(); 

                    }                
                }
            }); 
        }     
    }
    
    this.login = function()
    {
        var userName = $("#txtLoginUserName").val();
        var password = $("#txtLoginPassword").val();
        $.ajax({
            type:"POST",
            url:"/User/Login",
            data:"userName="+userName+"&password="+password,
            success:function(msg){
                var msg = msg.parseJSON();
                $("#loginMessage").show();
                if(msg.isSuccessful == false)
                {
                    $("#loginMessage").attr("innerText", msg.errorMessage);
                    
                }
                else{
                    $("#loginMessage").attr("innerText", "Welcome back, " + userName + " !");                    
                    window.setTimeout("", 2000);                                      
                    window.location.reload();         
                }
            }
        });        
    }
}
